//
//  FavStartView.m
//  Yogo
//
//  Created by WangLei on 15/7/22.
//  Copyright (c) 2015年 zhuxietong. All rights reserved.
//

#import "FavStartView.h"
//#import "SmoSky-Swift.h"

@implementation FavStartView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self loadStarts];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self loadStarts];
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _count = 0;
        _scolor = [UIColor orangeColor];
        [self loadStarts];
    }
    return self;
}



- (void)loadStarts
{
    
    
    NSInteger index = 0;
    
    for (; index < StarCount; index++) {
        UIView *b = [self viewWithTag:200+index];
        [b removeFromSuperview];
    }
    
    
    
    
    CGRect rect = self.frame;
    CGFloat distance = 5;
    
    CGFloat width = rect.size.width/StarCount;
    CGFloat startWidth = width-distance;
    
    CGFloat originY = (self.frame.size.height-startWidth)/2.0;
    CGFloat originX = width*0+distance/2.0;
    
    for (int i=0; i<StarCount; i++) {
        UIButton *startBt = [[UIButton alloc] initWithFrame:CGRectMake(originX, originY, startWidth, startWidth)];
        [startBt addTarget:self action:@selector(starButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        originX = originX+width;
        
//        AFLable * lable = [[AFLable alloc] initWithFrame:CGRectMake(0, 0, startWidth, startWidth)];
//        lable.font = [UIFont systemFontOfSize:startWidth];
//        lable.AFIndex = 547;
//        lable.textColor = self.scolor;
//        
//        UIImage *s_image = lable.shootImage;
//        
//        lable.textColor = [UIColor colorWithWhite:0.8 alpha:0.8];
//        UIImage *n_image = lable.shootImage;
        
        UIImage *s_image = [UIImage imageNamed:@"pj_x1"];
        UIImage *n_image = [UIImage imageNamed:@"pj_x2"];
        
        [startBt setBackgroundImage:n_image forState:UIControlStateNormal];
        [startBt setBackgroundImage:n_image forState:UIControlStateHighlighted];
        
        [startBt setBackgroundImage:s_image forState:UIControlStateSelected];
        startBt.tag = 200+i;
        [self addSubview:startBt];
    }
    
    //    [self setCount:1];
    [self updateCountState];
    
    self.backgroundColor = [UIColor clearColor];
}

- (void)starButtonAction:(UIButton*)sender
{
    
    NSInteger tag = sender.tag - 200;
    if (self.editAble) {
        if (sender.selected) {
            [self setCount:tag];
        }
        else
        {
            [self setCount:tag+1];
        }
    }
}

- (void)setCount:(NSInteger)count
{
    _count = count;
    [self updateCountState];
}

- (void)updateCountState
{
    for (int i=0; i<StarCount; i++) {
        UIButton *button = (UIButton*)[self viewWithTag:200+i];
        
        
        if (self.count<=i) {
            [button setSelected:NO];
        }
        else
        {
            [button setSelected:YES];
        }
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
